/**
 * 大数据按系统载体统计（单日）
 * @param startTime
 * @param endTime
 * @param systemType
 * @param page
 */
//从url中获取参数
var LocString=String(window.document.location.href);
 function GetQueryString(str){
	 var rs=new RegExp("(^|)"+str+"=([^&]*)(&|$)","gi").exec(LocString),tmp;
	 if(tmp=rs)return tmp[2];
	 return "没有这个参数";
 };
$(function(){
	var systemT=decodeURI(GetQueryString("systemType"));
	bySystemOneDay(systemT,"1");
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	}; 
	//每次点击的时候清空表格
	$('tbody > tr td').remove();
	$(".pageBox button").on("click",function(){
		var systemType=$("#systemType").val();
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
			}else{
				if(page==1){
					bySystemOneDay(systemType,page);
				}else if(page>showPageN-4){
				}else{
					bySystemOneDay(systemType,page);
				}
			}
		}
		$("input[name='selectPage']").val("");
	});
	
});
//查询
function selectBySystemOneDay(){
	$('tbody > tr td').remove();
	if(page==null || page.length<=0){
		page=1;
	}else{
		var page = $('.pagination .active a').text();
	}	
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var systemType = $("#systemType").val();
	var param = {
			"startTime" : startTime,
			"endTime" : endTime,
			"systemType" : systemType,
			"page" : page,
		}
	var aj = $.ajax({
		url : '/web-adv/bySystemOneDay',
		data : param,
		type : 'post',
		success : function(bigSystem) {
			$('tbody > tr td').remove();
			$('#pageBox').extendPagination({
				totalCount:bigSystem.total,
		 		showPage: bigSystem.pages+1,
	            limit: bigSystem.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	bySystemOneDay(systemType,page);	
	            }
			});
			$("#endTime").val(bigSystem.endTime);
			$("#startTime").val(bigSystem.startTime);
			$("#area").val(bigSystem.area);
			var beans = bigSystem.beans;
	
			for(var j = 0; j < beans.length;j++){
				if(typeof beans[j].clickNum=="object"){
					beans[j].clickNum=0;
				}
				//保留2位有效数字
				if(typeof beans[j].time=="object" ){
					beans[j].time="0000-00-00"
				}
				
				var averagePrice=parseFloat(beans[j].averagePrice).toFixed(2);
				var adPay=parseFloat(beans[j].adPay).toFixed(2);
				var adAllPay=parseFloat(beans[j].adAllPay).toFixed(2);
				var proportion=parseFloat((beans[j].proportion)*100).toFixed(2);
				var adBudget=parseFloat(beans[j].adBudget).toFixed(2);
				var tbodys = "<tr><td>"+(j+1)+"</td>" +
						"<td>"+beans[j].time.split(" ")[0]+"</td>" +
						"<td class='device'>"+beans[j].device+"</td>" +
						"<td class='adNum'>"+beans[j].adNum+"</td>" +
						"<td class='aPrice'>"+averagePrice+"</td>" +
						"<td class='adAllPay'>"+adAllPay+"</td>" +
						"<td class='adPay'>"+adPay+"</td>" +
						"<td>"+proportion+"%"+"</td>" +
						"<td class='adBudget'>"+adBudget+"</td>" +
						"<td class='percent'>"+(j+1)+"</td></tr>";
				$("#tb").append(tbodys);
			};
		}
	});
};
//单天的查询
function bySystemOneDay(sysT,page) {
	/*var startTime = GetQueryString("startTime");
	var endTime = GetQueryString("endTime");*/
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var systemType= sysT;
	var param = {
		"startTime" : startTime,
		"endTime" : endTime,
		"systemType" : systemType,
		"page" : page
	}
	var aj = $.ajax({
		url : '/web-adv/bySystemOneDay',
		data : param,
		type : 'post',
		success : function(data) {
			$('tbody > tr td').remove();
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	getPage(page);
	            }
			});
			$("#endTime").val(data.endTime.split("%")[0]);
			$("#startTime").val(data.startTime.split("%")[0]);
			$("#systemType").val(data.systemType);
			var beans = data.beans;
			for(var j = 0; j < beans.length;j++){
				var averagePrice=parseFloat(beans[j].averagePrice).toFixed(2);
				var adPay=parseFloat(beans[j].adPay).toFixed(2);
				var adAllPay=parseFloat(beans[j].adAllPay).toFixed(2);
				var proportion=parseFloat((beans[j].proportion)*100).toFixed(2);
				var adBudget=parseFloat(beans[j].adBudget).toFixed(2);
				var tbodys = "<tr><td>"+(j+1)+"</td>" +
						"<td>"+beans[j].time.split(" ")[0]+"</td>" +
						"<td class='device'>"+beans[j].device+"</td>" +
						"<td class='adNum'>"+beans[j].adNum+"</td>" +
						"<td class='aPrice'>"+averagePrice+"</td>" +
						"<td class='adAllPay'>"+adAllPay+"</td>" +
						"<td class='adPay'>"+adPay+"</td>" +
						"<td>"+proportion+"%"+"</td>" +
						"<td class='adBudget'>"+adBudget+"</td>" +
						"<td class='percent'>"+(j+1)+"</td></tr>";
				$("#tb").append(tbodys);
			}
		}
	});
};
/**
 * 获取后台传过来的page
 */
function  getPage(page){
	//每次点击的时候清空表格
	$('tbody > tr td').remove();
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var systemType = $("#systemType").val();
	var param = {
		"startTime" : startTime,
		"endTime" : endTime,
		"systemType" : systemType,
		"page" : page
	}
	var aj = $.ajax({
		url : '/web-adv/bysystemOneDay',
		data : param,
		type : 'post',
		success : function(data) {
			$('tbody > tr td').remove();
			if(data.beans.length==0){
//					alert("亲已经到最后一页了");
				//返回最后一页
			}
			$("#endTime").val(data.endTime);
			$("#startTime").val(data.startTime);
			var beans = data.beans;
			var sumAdpayNum=0;
			var budgetMoney=0;
			var adPayNum=0;
			var proportionNum=0;
			for(var j = 0; j < beans.length;j++){
				if(typeof beans[j].clickNum=="object"){
					beans[j].clickNum=0;
				}
				//保留2位有效数字
				if(typeof beans[j].time=="object" ){
					beans[j].time="0000-00-00"
				}
				
				var averagePrice=parseFloat(beans[j].averagePrice).toFixed(2);
				var adPay=parseFloat(beans[j].adPay).toFixed(2);
				var adAllPay=parseFloat(beans[j].adAllPay).toFixed(2);
				var proportion=parseFloat((beans[j].proportion)*100).toFixed(2);
				var adBudget=parseFloat(beans[j].adBudget).toFixed(2);
				var tbodys = "<tr><td>"+(j+1)+"</td>" +
						"<td>"+beans[j].time.split(" ")[0]+"</td>" +
						"<td class='device'>"+beans[j].device+"</td>" +
						"<td class='adNum'>"+beans[j].adNum+"</td>" +
						"<td class='aPrice'>"+averagePrice+"</td>" +
						"<td class='adAllPay'>"+adAllPay+"</td>" +
						"<td class='adPay'>"+adPay+"</td>" +
						"<td>"+proportion+"%"+"</td>" +
						"<td class='adBudget'>"+adBudget+"</td>" +
						"<td class='percent'>"+(j+1)+"</td></tr>";
				$("#tb").append(tbodys);
			}
		}
	});
};
//返回
function turn(){
	window.location.href="/web-adv/advWeb/views/bigData/index.html?selectedNav%3D4%26navPos%3D0";
};
